/**
 * @copyright 2017 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */
package com.sinping.qh.service.auth.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sinping.qh.repository.mysql.dao.RoleMapper;
import com.sinping.qh.service.auth.IRoleService;
import com.sinping.qh.domain.auth.Role;
import com.sinping.qh.domain.auth.RoleResource;
import com.sinping.qh.service.auth.IRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘庆沛
 * @since 2017-12-18
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Autowired
    private IRoleResourceService roleResourceService;

    @Override
    public void insertRoleAndRoleResource(Role role) {
        //插入角色后 需获取角色id
        this.insert(role);
        roleResourceService.insertByRole(role);
    }

    /**
     * 删除角色的时候，同时删除角色资源关系
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Serializable id) {
        EntityWrapper<RoleResource> wrapper=new EntityWrapper<RoleResource>();
        wrapper.where("role_id={0}",id);
        roleResourceService.delete(wrapper);
        return super.deleteById(id);
    }

    @Override
    public Role selectById(Serializable id) {
        Role role = super.selectById(id);
        if(role!=null){
            Wrapper<RoleResource> wrapper=new EntityWrapper<>();
            wrapper.where("role_id={0}",role.getId());
            List<RoleResource> roleResources = roleResourceService.selectList(wrapper);
            role.setResources(this.getResources(roleResources));
        }
        return role;
    }

    @Override
    public List<Role> selectList(Wrapper<Role> wrapper) {
        List<Role> roles = super.selectList(wrapper);
        this.setResources(roles);
        return roles;
    }

    @Override
    public Page<Role> selectPage(Page<Role> page) {
        Page<Role> rolePage = super.selectPage(page);
        List<Role> records = rolePage.getRecords();
        this.setResources(records);
        return rolePage;
    }

    @Override
    public boolean updateById(Role entity) {
        Wrapper<RoleResource> wrapper=new EntityWrapper<>();
        wrapper.where("role_id={0}",entity.getId());
        //role_id为null时 不能作为条件
//        Map<String,Object> params=new HashMap<>();
//        params.put("role_id",entity.getId());
        //删除之前角色资源关系，再插入
        roleResourceService.delete(wrapper);
        roleResourceService.insertByRole(entity);
        entity.setUpdateTime(new Date());
        return super.updateById(entity);
    }

    /**
     * 获取resource_id集合
     * @param roles
     * @return
     */
    private void setResources(List<Role> roles){
        if(roles!=null&&roles.size()>0){
            for(Role role:roles){
                Wrapper<RoleResource> wrapper=new EntityWrapper<>();
                wrapper.where("role_id={0}",role.getId());
                List<RoleResource> roleResources = roleResourceService.selectList(wrapper);
                role.setResources(this.getResources(roleResources));
            }
        }
    }

    /**
     * 获取resource_id集合
     * @param roleResources
     * @return
     */
    private List<String> getResources(List<RoleResource> roleResources){
        List<String> resourceIds=new ArrayList<>();
        if(roleResources!=null&&roleResources.size()>0){
            for(RoleResource roleResource:roleResources){
                resourceIds.add(roleResource.getResourceId());
            }
        }
        return resourceIds;
    }

}
